<template>
  <!--物资管理-预警设置-->
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="21">

          <a-col :xl="5" :lg="9" :md="11" :sm="24">
            <a-form-item label="物资名称">
              <a-input placeholder="请输入物资名称" class="input1" v-model="queryParam.platformProductName" style="width: 100%;"></a-input>
            </a-form-item>
          </a-col>

          <a-col :xl="5" :lg="8" :md="11" :sm="24">
            <a-form-item label="型号">
              <a-input placeholder="请输入型号" class="input1" v-model="queryParam.model" style="width: 100%;"></a-input>
            </a-form-item>
          </a-col>

          <a-col :xl="5" :lg="8" :md="11" :sm="24">
            <a-form-item label="项目">
              <a-select :options="levelOneOptions" placeholder="请输入项目" class="input1"
                v-model="queryParam.projectId"  style="width: 100%;"></a-select>
            </a-form-item>
          </a-col>

          <a-col :xl="4" :lg="6" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleYujin('add')" icon="plus" style="background-color:#32cd99;color: white;">新增预警</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <a-table ref="table" size="middle" v-bind="tableProps" bordered rowKey="id" class="j-table-force-nowrap"
        :scroll="{ x: true }" :columns="columns" :data-source="dataSource" :pagination="ipagination" :loading="loading"
        @change="handleTableChange">
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text, record">
          <span v-if="!text" style="font-size: 12px; font-style: italic">无图片</span>
          <img v-else :src="getImgView(text)" :preview="record.id" height="25px" alt=""
            style="max-width: 80px; font-size: 12px; font-style: italic" />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">无文件</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
            下载
          </a-button>
        </template>
        <span slot="status" slot-scope="text, record">
          <a-switch :checked="record.status === '1' ? true:false" @change="updata($event,record)"/>
        </span>
        <span v-if="!(record.status === '1')" slot="action" slot-scope="text, record">
          <a @click="handleYujin('edit',record)"> 配置预警</a>
        </span>
      </a-table>
    </div>
    <wt-yujin-modal ref="modalForm" @ok="modalFormOk" />
  </a-card>
</template>

<script>
  import {
    JeecgListMixin
  } from '@/mixins/JeecgListMixin'
  import WtYujinModal from './modules/WtYujinModal'
  import '@/assets/less/TableExpand.less'
  import {
    httpAction,
    getAction,
    postAction
  } from '@/api/manage'
  export default {
    name: 'WtWuziList',
    mixins: [JeecgListMixin],
    components: {
      WtYujinModal,
    },
    data() {
      return {
        description: '物资明细',
        // 表头
        columns: [
          {
            title: '项目',
            align: 'center',
            dataIndex: 'projectName',
          },
          {
            title: '物资名称',
            align: 'center',
            dataIndex: 'platformProductName',
          },
          {
            title: '型号',
            align: 'center',
            dataIndex: 'model',
          },
          {
            title: '单位',
            align: "center",
            dataIndex: 'unit'
          },
          {
            title: '计划量',
            align: 'center',
            dataIndex: 'planNum',
          }, {
            title: '当前采购量',
            align: 'center',
            dataIndex: 'amountPurchased',
          },
          {
            title: '最高值',
            align: 'center',
            dataIndex: 'maximum',
          }, {
            title: '库存剩余',
            align: 'center',
            dataIndex: 'inventory',
          },

          {
            title: '最低值',
            align: 'center',
            dataIndex: 'minimum',
          },
          {
            title: '状态',
            align: "center",
            dataIndex: 'status',
            scopedSlots: {
              customRender: 'status'
            }
          },
          {
            title: '操作',
            dataIndex: 'action',
            align: 'center',
            fixed: 'right',
            width: 147,
            scopedSlots: {
              customRender: 'action'
            },
          },
        ],
        url: {
          list: '/materials/wtMaterialsWarning/list',
          detail:'/materials/wtMaterialsWarning/queryById',
          edit:'/materials/wtMaterialsWarning/edit',
        },
        ipagination: {
          current: 1,
          pageSize: 10,
          pageSizeOptions: ['10', '20', '30', '50', '100', '200', '300', '400', '500', '600', '700', '1000'],
          showTotal: (total, range) => {
            return range[0] + '-' + range[1] + ' 共' + total + '条'
          },
          showQuickJumper: true,
          showSizeChanger: true,
          total: 0,
        },
        dictOptions: {},
        newArr: [],
        levelOneOptions: [],
        treeData1: [],
        dataSource: [],
        newDataSource: [],
        footerDataSource: [],
        pageSwitch: true,
        superFieldList: [],
      }
    },
    created() {
      this.getSuperFieldList()
    },
    mounted() {
      this.newDataSource = this.dataSource
    },
    methods: {
      //获取详情-编辑
      updata(row,record){
        let params = {
          id:record.id
        }
        getAction(this.url.detail, params).then((res) => {
            if (res.success) {
              let model = res.result;
              let httpurl = this.url.edit;
              if(model.status === '1'){
                model.status = '0';
              }else{
                model.status = '1';
              }
              httpAction(httpurl, model, 'POST').then((res2) => {
                  if (res2.success) {
                    this.$message.success(res2.message)
                    this.loadData(1);
                  } else {
                    this.$message.warning(res2.message)
                  }
                })
            } else {
              this.$message.warning(res.message)
            }
          })
        console.log(22,row,record)
      },
      handleYujin: function(type,record) {
        if(type === 'add'){
          this.$refs.modalForm.title = '新增';
          this.$refs.modalForm.pageType = type;
          this.$refs.modalForm.disabled = false;
          this.$refs.modalForm.add();
        }else if(type === 'edit'){
          this.$refs.modalForm.title = '配置预警';
          this.$refs.modalForm.pageType = type;
          this.$refs.modalForm.disabled = true;
          this.$refs.modalForm.edit(record);
        }
      },
      loadData(arg) {
        //加载数据 若传入参数1则加载第一页的内容
        if (arg === 1) {
          this.ipagination.current = 1
        }
        var params = this.getQueryParams() //查询条件
        console.log(222,params)
        this.loading = true
        getAction(this.url.list, params)
          .then((res) => {
            if (res.success) {
              //update-begin---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
              this.dataSource = res.result.records || res.result

              if (res.result.total) {
                this.ipagination.total = res.result.total
              } else {
                this.ipagination.total = 0
              }
              // this.dataHandling(1,this.ipagination.pageSize,this.dataSource)
              //update-end---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
            } else {
              this.$message.warning(res.message)
            }
          })
          .finally(() => {
            this.loading = false
          })
        getAction(`/sys/sysDepart/getDepartByOrgType?orgType=1`).then((res) => {
          if (res.success) {
            this.treeData1 = []
            let treeList1 = res.result
            for (let a = 0; a < treeList1.length; a++) {
              let temp = treeList1[a]
              temp.isLeaf = temp.leaf
              this.treeData1.push(temp)
            }

            this.levelOneOptions = [...this.treeData1].map((v) => {
              return {
                ...v,
                value: v.id,
                label: v.departName,
              }
            })
          }
        })
      },
      initDictConfig() {},
      getSuperFieldList() {
        let fieldList = []
        fieldList.push({
          type: 'string',
          value: 'name',
          text: '物资名称',
          dictCode: ''
        })
        fieldList.push({
          type: 'string',
          value: 'model',
          text: '型号',
          dictCode: ''
        })
        fieldList.push({
          type: 'string',
          value: 'number',
          text: '数量',
          dictCode: ''
        })
        fieldList.push({
          type: 'string',
          value: 'warehouseName',
          text: '仓库',
          dictCode: ''
        })
        fieldList.push({
          type: 'string',
          value: 'createTime',
          text: '时间',
          dictCode: ''
        })
        fieldList.push({
          type: 'string',
          value: 'type_dictText',
          text: '出库入库选项',
          dictCode: ''
        })

        this.superFieldList = fieldList
      },
    },
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';

  .input1 {
    width: 170px;
    height: 30px;
  }
</style>